<template>
  <div class="purchase-order-preparation-container page-main">
    <query-page
      ref="preparationPage"
      v-model:selectedKeys="selectedKeys"
      title="采购订单编制"
      :form-component-config="formComponentConfig"
      :form-render-config="formRenderConfig"
      :form-render-keys="formRenderKeys"
      :table-component-config="tableComponentConfig"
      :table-render-config="tableRenderConfig"
      :table-render-keys="tableRenderKeys"
      :loading="isLoading"
      :data="renderData"
      :total="total"
      @selection-change="selectionChange"
      @query-data="getData"
    >
      <template #container-side>
        <a-button
          v-permission="['pms:order:save']"
          type="primary"
          @click="openAdd"
          >新增</a-button
        >
      </template>

      <template #operate="{ record }">
        <a-button
          v-permission="['pms:order:update']"
          :hidden="record.ordStatus !== '0'"
          type="text"
          @click="edit(record)"
          >编辑</a-button
        >
        <a-button
          v-permission="['pms:order:approval']"
          :hidden="record.ordStatus !== '0'"
          type="text"
          @click="approval(record)"
          >提交审批</a-button
        >
        <a-button
          v-permission="['pms:order:update']"
          :hidden="record.ordStatus === '25'"
          type="text"
          @click="signInfoUpdate(record)"
          >签订信息更新</a-button
        >
        <a-button
          v-permission="['pms:order:sign']"
          :hidden="record.ordStatus !== '15'"
          type="text"
          @click="sign(record)"
          >签订</a-button
        >
        <a-button
          v-permission="['pms:order:revokesign']"
          :hidden="!(record.billStatus === '0' && record.ordStatus === '25')"
          type="text"
          @click="cancelSign(record)"
          >撤销签订</a-button
        >
        <a-button
          v-permission="['pms:order:delete']"
          :hidden="record.ordStatus !== '0'"
          type="text"
          @click="del(record)"
          >删除</a-button
        >
      </template>
    </query-page>
    <addDialog
      v-if="addVisible"
      :row-data="rowData"
      @close-drawer="closeDrawer"
      @success="success"
    />
    <approvalDialog v-if="approvalVisible" @close-drawer="closeDrawer" />
    <signDialog
      v-if="signVisible"
      :row-data="rowData"
      @close-drawer="closeDrawer"
      @success="success"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import queryPage from '@/common/components/query-page/index.vue';
  import { Message } from '@arco-design/web-vue';
  import { info } from '@/common/utils/modal-promise';
  import { hasDataAndBack, dateFormatter } from '@/common/utils/index';
  import {
    formRenderConfig,
    formComponentConfig,
  } from '@/views/purchase-order/components/formComp/baseFormConfig';
  import {
    tableRenderConfig,
    tableComponentConfig,
  } from '@/views/purchase-order/components/tableComp/baseTableConfig';
  import {
    getPmsOrderPage,
    delOrderById,
    orderSigning,
    orderSigningRevoke,
    orderApproval,
  } from '@/api/purchase-order/purchaseOrderPreparation';
  // import CollapseBtn from '@/common/components/collapse-btn/collapse-btn';
  import { baseRenderData } from './baseRenderData';
  import addDialog from './add/index.vue';
  import approvalDialog from './approval/index.vue';
  import signDialog from './sign/index.vue';

  const preparationPage = ref();
  const selectedKeys = ref([]);
  const isLoading = ref(true);
  const renderData = ref<any[]>([]);
  const addVisible = ref(false);
  const approvalVisible = ref(false);
  const signVisible = ref(false);
  const total = ref(0);
  const rowData = ref<any>(null);
  // 渲染检索项
  const formRenderKeys = ref<any[]>([...baseRenderData.formRenderKeys]);

  // 当前表格要渲染哪些值
  const tableRenderKeys = ref<any[]>([
    ...baseRenderData.tableRenderKeys,
    'operate',
  ]);
  // 添加
  const openAdd = () => {
    rowData.value = null;
    addVisible.value = true;
  };
  // // 保存
  // const save = () => {
  //   console.log('1111');
  // };
  // 编辑
  const edit = (row: any) => {
    rowData.value = row;
    addVisible.value = true;
  };
  // 签订信息更新
  const signInfoUpdate = (row: any) => {
    rowData.value = row;
    signVisible.value = true;
  };
  // 提交审批
  const approval = async (row: any) => {
    await info({
      title: '您确定要提交审批所选的数据？',
      content: '',
      hideCancel: false,
    });
    const res: any = await orderApproval(row.id);
    if (res.code === 200) {
      Message.success('提交审批成功');
      getData(preparationPage.value.getStatus());
    }
    // approvalVisible.value = true;
  };
  // 签订
  const sign = async (row: any) => {
    if (!row.signUserName || !row.signDateTime) {
      Message.warning(`【签订人】和【签订日期】不能为空`);
      return;
    }
    try {
      await info({
        title: '您确定要签订该条数据？',
        content: '',
        hideCancel: false,
      });
    } catch (error) {
      return;
    }
    const res: any = await orderSigning(row);
    if (res.code === 200) {
      Message.success('签订成功');
      getData(preparationPage.value.getStatus());
    } else {
      Message.error('签订失败');
    }
  };
  // 撤销签订
  const cancelSign = async (row: any) => {
    if (row.billStatus !== '0') {
      Message.warning(
        `该条数据单据状态为【${row.billStatusName}】，不能撤销签订！`
      );
      return;
    }
    try {
      await info({
        title: '您确定要撤销签订该条数据？',
        content: '',
        hideCancel: false,
      });
    } catch (error) {
      return;
    }
    const res: any = await orderSigningRevoke(row);
    if (res.code === 200) {
      Message.success('撤销签订成功');
      getData(preparationPage.value.getStatus());
    }
  };
  // 删除
  const del = async (row: any) => {
    await info({
      title: '您确定要删除当前数据？',
      content: '',
      hideCancel: false,
    });
    const res: any = await delOrderById(row.id);
    if (res.code === 200) {
      Message.success('删除成功');
      getData(preparationPage.value.getStatus());
    }
  };
  const getData = async (queryInfo: object) => {
    renderData.value = [];
    isLoading.value = true;
    const params: any = { ...queryInfo };
    if (params.signDateTime) {
      params.signDateTime = dateFormatter(
        params.signDateTime[0],
        params.signDateTime[1]
      );
    }
    if (params.effectiveBegDateTime) {
      params.effectiveBegDateTime = dateFormatter(
        params.effectiveBegDateTime[0],
        params.effectiveBegDateTime[1]
      );
    }
    if (params.effectiveEndDateTime) {
      params.effectiveEndDateTime = dateFormatter(
        params.effectiveEndDateTime[0],
        params.effectiveEndDateTime[1]
      );
    }
    const res: any = await getPmsOrderPage(params);
    if (res.code === 200) {
      total.value = res.data.total;
      renderData.value = res.data.list.map((item: any) => {
        item.isEidt = false;
        return item;
      });
      refreshNumData(renderData.value, total.value);
    }
    isLoading.value = false;
  };
  const refreshNumData = (list: any[], tNum: any) => {
    if (hasDataAndBack(list, tNum, preparationPage.value?.getStatus().pageNo)) {
      preparationPage.value?.setCurrent(
        preparationPage.value.getStatus().pageNo - 1
      );
      getData(preparationPage.value.getStatus());
    }
  };
  const selectionChange = (val: string[]) => {
    console.log('selectedKeys', val);
  };
  // 初期话
  const initData = () => {
    console.log('rowBtnList.value');
  };
  onMounted(() => {
    initData();
  });
  // 关闭弹窗
  const closeDrawer = () => {
    addVisible.value = false;
    signVisible.value = false;
    approvalVisible.value = false;
    getData(preparationPage.value.getStatus());
  };
  // 关闭弹窗
  const success = () => {
    addVisible.value = false;
    signVisible.value = false;
    initData();
    getData(preparationPage.value.getStatus());
  };
</script>

<style lang="less" scoped>
  .purchase-order-preparation-container {
    height: calc(100vh - 125px);
    // margin: 16px;
    // background: #fff;
    // border: 1px solid #e4e7ed;
    // border-radius: 8px;
  }
</style>
./collapse-btn
